﻿namespace com.stifel.ServerBuildMgmt.BuildMgmtMoniterNetworkInfo
{
    using System;
    using System.Collections.Generic;
    using System.Web;
    using System.Web.UI;
    using System.Web.UI.WebControls;

    using com.stifel.Extensions.StringExtensions;
    using com.stifel.ServerBuildMgmtDao;

    public partial class BuildMgmtMoniterNetworkInfoUserControl : UserControl
    {
        #region Methods

        protected void onItemCommand( object sender, RepeaterCommandEventArgs e )
        {
            if ( e.CommandName == "Edit" )
            {
                Summary s = new Summary( );
                ServerValues server = e.CommandArgument.ToString( ).FromBase64String( ).DeserializeFromXml<ServerValues>( );
                uiCurrentSever.Value = e.CommandArgument.ToString( );
                Label1.Text = s.getSummaryWithOutOriginalValues( server );
                if ( server.MoniteringNetwork.CompletedFlag.HasValue )
                {
                    uiCbNetworkCompletedFlag.Checked = server.MoniteringNetwork.CompletedFlag.Value;
                }
                else
                {
                    uiCbNetworkCompletedFlag.Checked = false;
                }
                //TextBox1.Text = server.MoniteringNetwork.Text1;
                //TextBox2.Text = server.MoniteringNetwork.Text2;
                //uiValMoniterNetworkNotes.Text = server.MoniteringNetwork.Note;
                uiMoniterNetwork.Visible = false;
                uiMoniterNetworkUpdatePanel.Visible = true;
                setDefaultButton( );
            }
        }

        protected void Page_Load( object sender, EventArgs e )
        {
            if ( !Page.IsPostBack )
            {
                isChanged.Value = false.ToString( );
                if ( HttpContext.Current.Request.QueryString[ "ItemGuid" ] == null )
                {
                    PageLoadBackUP( );
                }
                else if ( HttpContext.Current.Request.QueryString[ "ItemGuid" ] == string.Empty )
                {
                    PageLoadBackUP( );
                }
                else
                {
                    try
                    {
                        Guid ItemGuid = HttpContext.Current.Request.QueryString[ "ItemGuid" ].ToGuid( );
                        StifelServerBuildDao dao = new StifelServerBuildDao( );
                        StifelVirtualServerRequest request = dao.getRequestFromListUsingGUID( ItemGuid );
                        if ( request != null )
                        {
                            uiLitValRequest.Text = request.Id.Value.ToString( );
                            Summary s = new Summary( );
                            VirtualServerCollectionClass v = request.getRequestInfoDeserialized( );
                            List<RepeaterBinding> summaryBinder = s.getSummaryWithOutOriginalValues( v );
                            if ( request.getMoniteringNetworkStatus( ) == true )
                            {
                                myRepeater.DataSource = summaryBinder;
                                myRepeater.DataBind( );
                                uiMoniterNetwork.Visible = true;
                                uiMoniterNetworkUpdatePanel.Visible = false;
                                uiBackBadUrlRequest.Visible = false;
                                setSubmitValidation( );
                            }
                            else
                            {
                                myRepeater.DataSource = summaryBinder;
                                myRepeater.DataBind( );
                                uiMoniterNetwork.Visible = true;
                                uiMoniterNetworkUpdatePanel.Visible = false;
                                uiBackBadUrlRequest.Visible = false;
                                foreach ( RepeaterItem r in myRepeater.Items )
                                {
                                    ImageButton ie = ( ImageButton ) r.FindControl( "editButton" );
                                    ie.ImageUrl = "~/_layouts/com.stifel.ServerBuildMgmt/images/editg20x20grey.png";
                                    ie.Enabled = false;
                                }
                                uiBtnSave.Enabled = false;
                                uiBtnSubmit.Enabled = false;
                                uiLitNotification.Text = "Cannot Make Any Changes.";
                            }
                        }
                    }
                    catch ( Exception ex )
                    {
                        PageLoadBackUP( );
                        string errorMsg = ex.Message;
                    }
                }
                isSomethingChanged( );
                setUrlReferrer( );
            }
        }

        protected void uiBtnCancel_Click( object sender, EventArgs e )
        {
            Page.Response.Redirect( UrlReferrer );
        }

        protected void uiBtnSave_Click( object sender, EventArgs e )
        {
            SaveServers( );
        }

        protected void uiBtnSubmit_Click( object sender, EventArgs e )
        {
            SubmitServers( );
        }

        protected void uiCancelButton_Click( object sender, ImageClickEventArgs e )
        {
            uiMoniterNetwork.Visible = true;
            uiMoniterNetworkUpdatePanel.Visible = false;
            uiBackBadUrlRequest.Visible = false;
        }

        protected void uiNavigationTab_Click( object sender, EventArgs e )
        {
            Button button = sender as Button;
            if ( button != null )
            {
                string arg = button.CommandArgument;
                navigateToSelectedView( button );
                //summarize( arg );
            }
        }

        protected void uiOkButton_Click( object sender, ImageClickEventArgs e )
        {
            ServerValues server = uiCurrentSever.Value.FromBase64String( ).DeserializeFromXml<ServerValues>( );
            VirtualServerCollectionClass v = new VirtualServerCollectionClass( );
            foreach ( RepeaterItem r in myRepeater.Items )
            {
                HiddenField hiddenField = ( HiddenField ) r.FindControl( "valuePart" );
                ServerValues currentCompare = hiddenField.Value.FromBase64String( ).DeserializeFromXml<ServerValues>( );
                if ( currentCompare.ServerBasics.ServerName.Equals( server.ServerBasics.ServerName ) )
                {
                    if ( uiCbNetworkCompletedFlag.Checked )
                    {
                        currentCompare.MoniteringNetwork.CompletedFlag = true;
                    }
                    else
                    {
                        currentCompare.MoniteringNetwork.CompletedFlag = false;
                    }
                    //currentCompare.MoniteringNetwork.Text1 = TextBox1.Text;
                    //currentCompare.MoniteringNetwork.Text2 = TextBox2.Text;
                    //currentCompare.MoniteringNetwork.Note = uiValMoniterNetworkNotes.Text;
                }
                v.virtualServerCollection.Add( currentCompare );
            }
            Summary s = new Summary( );
            List<RepeaterBinding> summaryBinder = s.getSummaryWithOutOriginalValues( v );
            myRepeater.DataSource = summaryBinder;
            myRepeater.DataBind( );
            uiMoniterNetwork.Visible = true;
            uiMoniterNetworkUpdatePanel.Visible = false;
            uiBackBadUrlRequest.Visible = false;
            isChanged.Value = true.ToString( );
            isSomethingChanged( );
            setSubmitValidation( );
        }

        #endregion Methods

        #region Other

        //protected void uiCbNetworkCompletedFlag_CheckedChanged( object sender, EventArgs e )
        //{
        //    //summarize( uiBtnNetworkInfo.CommandArgument );
        //}

        #endregion Other
    }
}